﻿using System;
using System.IO;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace FileExplorer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitTreeView();
            InitListView();
        }

        private void InitTreeView()
        {
            string[] drives = Directory.GetLogicalDrives();
            foreach (string drive in drives)
            {
                TreeNode node = new TreeNode(drive);
                node.Nodes.Add("");
                treeView1.Nodes.Add(node);
            }
        }

        private void InitListView()
        {
            listView1.Columns.Add("Name", 200);
            listView1.Columns.Add("Size", 80);
            listView1.Columns.Add("Type", 80);
            listView1.View = View.Details;
        }

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes[0].Text == "")
            {
                e.Node.Nodes.Clear();
                try
                {
                    string[] dirs = Directory.GetDirectories(e.Node.FullPath);
                    foreach (string dir in dirs)
                    {
                        TreeNode node = new TreeNode(Path.GetFileName(dir));
                        node.Nodes.Add("");
                        e.Node.Nodes.Add(node);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    // ignore access denied directories
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            try
            {
                string[] dirs = Directory.GetDirectories(e.Node.FullPath);
                foreach (string dir in dirs)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(dir));
                    item.SubItems.Add("");
                    item.SubItems.Add("Folder");
                    listView1.Items.Add(item);
                }
                string[] files = Directory.GetFiles(e.Node.FullPath);
                foreach (string file in files)
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(file));
                    item.SubItems.Add(new FileInfo(file).Length.ToString());
                    item.SubItems.Add(Path.GetExtension(file));
                    listView1.Items.Add(item);
                }
            }
            catch (UnauthorizedAccessException)
            {
                // ignore access denied directories
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string filePath = Path.Combine(treeView1.SelectedNode.FullPath, listView1.SelectedItems[0].Text);
                if (File.Exists(filePath) && Path.GetExtension(filePath).ToLower() == ".txt")
                {
                    System.Diagnostics.Process.Start(filePath);
                }
            }
        }
    }
}
